package com.cms.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.kit.Constant;
import com.cms.common.kit.StrKit;
import com.cms.common.kit.Tools;
import com.cms.common.kit.Tree;
import com.cms.common.util.pass.CryptUtils;
import com.cms.common.util.pass.SystemDecoder;
import com.cms.controller.BaseController;
import com.cms.model.Admin;
import com.cms.model.Res;


@Controller
@RequestMapping(value = "/admin")
public class IndexController extends BaseController {

	
	@RequestMapping(value = "/index")
	public String index(Map<String, Object> map,HttpServletRequest request) {
		String admin_id=getCookie(request, Constant.CURRENT_ADMIN_COOKIE_INFO);
		if(StrKit.isBlank(admin_id)){
			admin_id=request.getAttribute("aid")+"";
			if(StrKit.isBlank(admin_id)){
				return "admin/login";
			}
		}else{
			admin_id=SystemDecoder._Decrypt(admin_id);
		}
		Admin admin = Admin.dao.findById(admin_id);
		map.put("admin_bean", admin);
		//生成有层级关系的权限菜单列表，此时列表并不包括功能性按钮
		String sql="select id,text,url,pId from s_res where type=1 and id in (select res_id from s_res_role where role_id=(select role_id from s_role_admin where admin_id=?))  order by seq";
		List<Res> res_tree = Res.dao.find(sql,admin_id);
		List<Tree> transferTree = Tools.transferTree(res_tree, 0);
		map.put("admin_menu", transferTree);
		return "admin/index";
	}
	
	@RequestMapping(value = "/desktop")
	public String desktop(Map<String, Object> map,HttpServletRequest request) {
		
		return "admin/desktop";
	}
	
	@RequestMapping(value = "/login")
	public String login(Map<String, Object> map,String username,String password,String rem,HttpServletResponse response, HttpServletRequest request) {
		map.put("username", username);
		map.put("rem", rem);
		if(StrKit.isBlank(username)||StrKit.isBlank(password)){
			map.put("errorMsg","用户名，密码及验证码为必填项！");
			return "admin/login";
		}
		Admin admin = Admin.dao.findFirst("select * from s_admin where loginName=? limit 1", username);
		if(admin==null){
			map.put("errorMsg","不存在该用户！");
			return "admin/login";
		}
		if(!StrKit.isEqual(CryptUtils.parseStrToMd5L32(password),admin.getStr("password"))){
			map.put("errorMsg","密码不正确!");
			return "admin/login";
		}
		if(admin.getInt("state")!=1){
			map.put("errorMsg","该用户已经别冻结，请联系管理员！");
			return "admin/login";
		}
		String remortIP = getRemortIP(request);
		HashMap<String, Object> hashMap = new HashMap<String,Object>();
		hashMap.put("id", admin.getInt("id")+"");
		hashMap.put("loginIp", remortIP);
		hashMap.put("lastLoginDate",StrKit.getCurrentDateStr());
		hashMap.put("loginCount",admin.getInt("loginCount")+1+"" );
		getModel(Admin.class,hashMap).update();
		String _cookie = SystemDecoder._Encrypt(admin.getInt("id")+"");
		if(rem=="1"){
			this.setCookie(response,Constant.CURRENT_ADMIN_COOKIE_INFO,_cookie,60*60*24*7);
		}else{
			this.setCookie(response,Constant.CURRENT_ADMIN_COOKIE_INFO,_cookie,-10);
		}
		request.setAttribute("aid", admin.get("id"));
		return "forward:/admin/index";
		
	}
	
	@RequestMapping(value = "/toLogin")
	public String toLogin(Map<String, Object> map,HttpServletRequest request) {
		
		return "admin/login";
	}
	
	@RequestMapping(value = "/loginOut")
	public String loginOut(Map<String, Object> map,HttpServletResponse response,HttpServletRequest request) {
		this.delCookie(response,Constant.CURRENT_ADMIN_COOKIE_INFO);
		return "admin/login";
	}
	
}
